package com.agileai.portal.controller.forum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.forum.ForumSectionInfoTreeManage;
import com.agileai.portal.bizmoduler.forum.UserLevel8BBSUserInfoManage;
import com.agileai.portal.extend.forum.ForumPrivilegeInterceptor;

/**
 * generated by miscdp
 */
public class ForumPostHandler extends SimpleHandler{
	private static String HOTSPOT = "hotspot";
	private static String INTEGRALCHANGE = "INTEGRAL_CHANGE";
	private static String POSTINGS = "POSTINGS";
	private static String ORDINARYPOSTS = "ORDINARY_POSTS";
	private static String N = "N";
	public ForumPostHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String responseText = " ";
		String resouce =  param.get("resouce");
		String maxCountValue = param.get("maxCount");
		int maxCount = Integer.parseInt(maxCountValue);
		JSONObject jsonObject = new JSONObject();
		ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
		List<DataRow> records = new ArrayList<DataRow>();
		if(HOTSPOT.equals(resouce) || HOTSPOT == resouce){
			records = forumSectionInfoTreeManage.findHotPostRecords();
		}else{
			records = forumSectionInfoTreeManage.findNewPostRecords();
		}
		try {
			int count = (maxCount > records.size()) ? records.size():maxCount;
			JSONArray jsonArray = new JSONArray();
			jsonObject.put("records", jsonArray);
			for (int i=0;i < count;i++){
				DataRow row = records.get(i);
				JSONObject tempJsonObject = new JSONObject();
				String fpmId = row.stringValue("FPM_ID");
				String fpmTitle = row.stringValue("FPM_TITLE");
				String fpmType = row.getString("FPM_TYPE");
				jsonArray.put(tempJsonObject);
				tempJsonObject.put("fpmId", fpmId);
				tempJsonObject.put("fpmTitle", fpmTitle);
				tempJsonObject.put("fpmType", fpmType);
			}
			String content = jsonObject.toString();
			responseText = content;
			
			this.setAttribute("content",jsonObject.toString());
		} catch (Exception e) {
			this.log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer mobileCreatePostForumInfo (DataParam param){
		String responseText = FAIL;
    	try {
        	String inputString = this.getInputString();
        	JSONObject jsonObject = new JSONObject(inputString);
        	String sectionCode = jsonObject.get("fciCode").toString();//板块编码
        	String fpmTitle = jsonObject.get("title").toString();//标题
        	String fpmExpression = jsonObject.get("fpmExpression").toString();//帖子类型
        	String fpmRewardPoints = jsonObject.get("points").toString();//分数
        	String fpmContent = jsonObject.get("fpmContent").toString();//内容
        	
        	ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
    		User user = (User)this.getUser();
    		String userCode = user.getUserCode();
    		
        	String fuLevel = ForumPrivilegeInterceptor.getFuLevel(user);
    		String fuIntegral = ForumPrivilegeInterceptor.getFuIntegral(user);
    		String fuLevelVal = forumSectionInfoTreeManage.getFuLevelValRecord(fuLevel);
    		String userId = forumSectionInfoTreeManage.getFuIdRecord(userCode);
    		String postScores = forumSectionInfoTreeManage.getPostScores(INTEGRALCHANGE,POSTINGS);
    		DataRow sectionInfoRow = forumSectionInfoTreeManage.getSectionInfo(sectionCode);
    		String sectionId = sectionInfoRow.getString("FCI_ID");
    		
        	int fuIntegralVal = 0;
    		if("".equals(fpmRewardPoints) || null == fpmRewardPoints){
    			fpmRewardPoints = "";
    			fuIntegralVal = Integer.valueOf(fuIntegral) + Integer.valueOf(fuLevelVal) * Integer.valueOf(postScores);
    		}else{
    			int fpmRewardPointsNum = Integer.valueOf(fpmRewardPoints);
    			fuIntegralVal = Integer.valueOf(fuIntegral) + Integer.valueOf(fuLevelVal) * Integer.valueOf(postScores)  - fpmRewardPointsNum;
    		}
        	
    		String fuIntegralStr = String.valueOf(fuIntegralVal);
    		UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = this.lookupService(UserLevel8BBSUserInfoManage.class);
    		userLevel8BBSUserInfoManage.updateUserFuIntegral(userId,fuIntegralStr);
    		
        	DataParam dataParam = new DataParam();
    		dataParam.put("FPM_ID", KeyGenerator.instance().genKey());
    		dataParam.put("FCI_ID",sectionId);
    		dataParam.put("FPM_USERID",userId);
    		dataParam.put("FPM_EXPRESSION",fpmExpression);
    		dataParam.put("FPM_TITLE",fpmTitle);
    		dataParam.put("FPM_CONTENT",fpmContent);
    		dataParam.put("FPM_CREATE_TIME", new Date());
    		dataParam.put("FPM_CLICK_NUMBER", 0);
    		dataParam.put("FPM_REWARD_POINTS", fpmRewardPoints);
    		dataParam.put("FPM_IS_TOP", N);
    		dataParam.put("FPM_SECTION_TOP", N);
    		dataParam.put("FPM_TYPE", ORDINARYPOSTS);
    		forumSectionInfoTreeManage.createPostMessageRecord(dataParam);
        	responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
    	return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer mobileCreateRepleceForumInfo(DataParam param){
		String responseText = FAIL;
		try {
        	String inputString = this.getInputString();
        	JSONObject jsonObject = new JSONObject(inputString);
        	String fpmId = jsonObject.get("fpmId").toString();
        	String replyFloor = jsonObject.get("replyFloor").toString();//回复楼数
        	String friContent = jsonObject.get("friContent").toString();
        	
        	User user = (User)this.getUser();
    		String userCode = user.getUserCode();
        	
        	ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
        	String fuId = forumSectionInfoTreeManage.getFuIdRecord(userCode);
        	
        	DataParam dataParam = new DataParam();
    		dataParam.put("FRI_ID", KeyGenerator.instance().genKey());
    		dataParam.put("FRI_CONTENT",friContent);
    		dataParam.put("FRI_USERID",fuId);
    		dataParam.put("FPM_ID",fpmId);
    		dataParam.put("FRI_REPORT_NUM",replyFloor);
    		dataParam.put("FRI_TIME", new Date());
    		
    		forumSectionInfoTreeManage.createRepliesInfoRecord(dataParam);
        	responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
    	return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer mobileUpdatePostClickNumber(DataParam param) {
		String responseText = FAIL;
		try {
			String inputString = this.getInputString();
        	JSONObject jsonObject = new JSONObject(inputString);
        	String fpmId = jsonObject.get("fpmId").toString();
        	String fpmClickNumberStr = jsonObject.get("fpmClickNumber").toString();
			int fpmClickNumber = Integer.valueOf(fpmClickNumberStr);
			fpmClickNumber = fpmClickNumber + 1;
			ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
			forumSectionInfoTreeManage.updateFpmClickNumberRecord(fpmId, fpmClickNumber);
			responseText = SUCCESS;
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer mobileCreateForumFavorite(DataParam param) {
		String responseText = FAIL;
		try {
			String inputString = this.getInputString();
        	JSONObject jsonObject = new JSONObject(inputString);
        	String fpmId = jsonObject.get("fpmId").toString();
        	User user = (User)this.getUser();
    		String userCode = user.getUserCode();
        	ForumSectionInfoTreeManage forumSectionInfoTreeManage = this.lookupService(ForumSectionInfoTreeManage.class);
        	String fuId = forumSectionInfoTreeManage.getFuIdRecord(userCode);
        	DataRow row = forumSectionInfoTreeManage.getForumPostMessageRecord(fpmId);
        	String fpmTitle = row.getString("FPM_TITLE");
        	int count = forumSectionInfoTreeManage.getForumFavoriteRecord(fpmId, fuId);
        	if(count == 0){
        		forumSectionInfoTreeManage.createForumFavoriteRecord(fpmId, fuId, fpmTitle);
        	}else{
        		forumSectionInfoTreeManage.delForumFavoriteRecord(fpmId, fuId);
        	}
        	responseText = SUCCESS;
		} catch (Exception e) {
			this.log.error(e.getMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
}